package com.zbkj.admin.controller.dev;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zbkj.dal.base.constants.OrderConstants;
import com.zbkj.dal.base.enums.BooleanEnums;
import com.zbkj.dal.base.enums.FundAccountType;
import com.zbkj.dal.base.result.CommonResult;
import com.zbkj.dal.common.utils.CrmebUtil;
import com.zbkj.dal.dao.*;
import com.zbkj.dal.entity.fund.UserFundAccount;
import com.zbkj.dal.entity.product.ProductBrand;
import com.zbkj.dal.entity.user.Employee;
import com.zbkj.dal.entity.user.User;
import com.zbkj.dal.entity.user.UserQuotaAccount;
import com.zbkj.dal.feign.client.WeWorkFeignClient;
import com.zbkj.service.service.PayCallbackService;
import com.zbkj.service.service.SystemUserLevelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping(value = "/dev")
public class DevController {

    @Resource
    private UserDao userDao;

    @Resource
    private UserQuotaAccountDao userQuotaAccountDao;

    @Resource
    private UserFundAccountDao userFundAccountDao;

    @Resource
    private SystemUserLevelService systemUserLevelService;

    @Resource
    private EmployeeDao employeeDao;

    @Resource
    private EmployeeAccountDao employeeAccountDao;

    @Resource
    private MerchantEmployeeDao merchantEmployeeDao;

    @Resource
    private WeWorkFeignClient weWorkFeignClient;

    @Resource
    private PayCallbackService payCallbackService;

    @GetMapping("/test")
    public void test() {
        payCallbackService.wechatPayCallback("{\"id\":\"1596cf97-0b64-5c99-a614-a76072714761\",\"create_time\":\"2024-04-05T15:12:30+08:00\",\"resource_type\":\"encrypt-resource\",\"event_type\":\"TRANSACTION.SUCCESS\",\"summary\":\"支付成功\",\"resource\":{\"original_type\":\"transaction\",\"algorithm\":\"AEAD_AES_256_GCM\",\"ciphertext\":\"jFQrinG3m+ETmLe6ZehlBfiyTmXoemhihX7aSre1EXx+4AeShwy11Mg/mjZ9ajdjb4P55O+uBzfIkHddP5SO8MLWJkIVu/OGoSoWRa7r3pdq9ScP1X/tZscYY+28yuGPp+e0Wd8s0eS3iVizn1j0hkW6MdfYnF3n5bwCGd5ToErIHvBIp+J80mQvpDGXWMivpkIi8XpB/gWha2pToyYcZ6GZfr4FBBzmbvfrcDw9rRhBJFB2r9bHzPUBslBnasgfJhQE20Y0BNC+MtBhzgg9WkKqvICpxy6UeL3nCIbYr8Wqx/FR22z4NlFpYZ6BCovWBGDdAFVhQsTeeuXPAq2ldPy6we5YDJp4crJqg8rX0dSciZMUw1ygafG3jn4Ak+fjp+kczFt8Jp/xCD6sMIxZqeQGNevLKI4SLVP4lsG8+AFRfXo9zFxd5M/QoQcj4xjFVhJ8csjmn2RSK8eOHzO5aR854RuN9XLyrGOzxNS2Di+H9W/G4kZ9WhyhvW1AM2Ov53jMKDWTHvl97cbRIvMHN/EFr1OsdGsn3dNaUud1EUuwan9T+vvZKVIiGWsvc6H6GGkxiI+9bH7y3cD3sm07HbNTUPI0AOY4uLPHwzu7d7AwdTdp9KDT\",\"associated_data\":\"transaction\",\"nonce\":\"7lNYJazwsmtS\"}}",
                "1712301150", "p1311zeLECzwGUHZzed7qN5oqNV6fNWo", "InXZ0PCWJDMQtVKfkYFX6GRQBTW0c1LdY3e12uSPED5J8zudpBhcnGPNU8WyJ8ryyZLs6B3l+/c1bRXe00Kdob/84UjFAJml+KLthuWRWg5966h0J2M0CeeLcBInSdzqXtAZTmn1X0w2OhhRZcnjd4A/kq6LXO1K8Ge0OQLuWc0dFqJbSeJ8ksvFmx8juSKZe60TkhoJXafpiiqFrL0W7HSjtcbpU6+jC4/TATMhOb3G+mEbdf/rMoaiznExg1v8mSEeOallih7pLlEiSNcLJSEZ2EInalRmROuayYkClnfc4LcHGajlic8y5wFmqvnaUpx8k/DcdVmAL+atbXfrOA==",
                "28CE902C24EEEE96A3842A9693941EB5A2E02E47");
    }

    @GetMapping("/update/employee-no")
    public CommonResult<Void> updateEmployee(@RequestParam("oldEmployeeNo") String oldEmployeeNo, @RequestParam("newEmployeeNo") String newEmployeeNo) {
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getEmployeeNo, oldEmployeeNo);
        Employee employee = employeeDao.selectOne(wrapper);
        if (Objects.isNull(employee)) {
            return CommonResult.failed("指定员工不存在");
        }
        employee.setEmployeeNo(newEmployeeNo);
        employee.setUpdateTime(LocalDateTime.now());

        weWorkFeignClient.updateEmployeeAlias(employee.getWxWorkAccount(), newEmployeeNo);

        employeeAccountDao.updateEmployeeNo(oldEmployeeNo, newEmployeeNo);
        merchantEmployeeDao.updateEmployeeNo(oldEmployeeNo, newEmployeeNo);
        employeeDao.updateById(employee);
        return CommonResult.success();
    }

    @GetMapping("/user/crab-roe/init")
    public CommonResult<Integer> userCrabRoeInit() {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        List<User> users = userDao.selectList(wrapper);
        List<UserFundAccount> userFundAccounts = users.stream().map(user -> {
            UserFundAccount userFundAccount = new UserFundAccount();
            userFundAccount.setFundAccount(CrmebUtil.getAccountNo(OrderConstants.CRAB_ROE_ACCOUNT_PREFIX, user.getId()));
            userFundAccount.setUserId(user.getId());
            userFundAccount.setAccountType(FundAccountType.CRAB_ROE.name());
            userFundAccount.setTotalAmount(user.getNowMoney());
            userFundAccount.setAvailableAmount(user.getNowMoney());
            userFundAccount.setFrozenAmount(BigDecimal.ZERO);
            userFundAccount.setCreateTime(LocalDateTime.now());
            userFundAccount.setUpdateTime(LocalDateTime.now());
            userFundAccount.setDeleted(BooleanEnums.FALSE.getCode());
            return userFundAccount;
        }).collect(Collectors.toList());
        return CommonResult.success(userFundAccountDao.batchInsert(userFundAccounts));
    }

    @GetMapping("/user/quota/init")
    public CommonResult<Integer> userQuotaInit() {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        List<User> users = userDao.selectList(wrapper);
        List<UserQuotaAccount> userQuotaAccounts = users.stream().map(user -> {
            UserQuotaAccount userQuotaAccount = new UserQuotaAccount();
            userQuotaAccount.setUserId(user.getId());
            userQuotaAccount.setQuotaAccount(CrmebUtil.getAccountNo(OrderConstants.QUOTA_ACCOUNT_PREFIX, user.getId()));
            userQuotaAccount.setQuotaLevel(systemUserLevelService.queryLevel(user.getCreditAmount()));
            userQuotaAccount.setQuotaTotal(user.getCreditAmount());
            userQuotaAccount.setQuotaBalance(user.getCreditAmount());
            userQuotaAccount.setQuotaUsed(BigDecimal.ZERO);
            userQuotaAccount.setEnable(BooleanEnums.TRUE.getCode());
            userQuotaAccount.setUpgraded(Objects.equals(user.getLevel(), 1) ? BooleanEnums.FALSE.getCode() : BooleanEnums.TRUE.getCode());
            userQuotaAccount.setCreateTime(LocalDateTime.now());
            userQuotaAccount.setUpdateTime(LocalDateTime.now());
            userQuotaAccount.setDeleted(BooleanEnums.FALSE.getCode());
            return userQuotaAccount;
        }).collect(Collectors.toList());
        return CommonResult.success(userQuotaAccountDao.batchInsert(userQuotaAccounts));
    }

    @Resource
    private ProductBrandDao productBrandDao;

    @GetMapping("/brand/pinyin")
    public CommonResult<Void> brandPinyin() {
        List<ProductBrand> productBrands = productBrandDao.selectList(new LambdaQueryWrapper<>());
        productBrands.forEach(productBrand -> {
            productBrand.setNamePy(PinyinUtil.getPinyin(productBrand.getName()).replaceAll(" ", ""));
            productBrandDao.updateById(productBrand);
        });
        return CommonResult.success();
    }
}
